package com.example.admin_oa.model.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.example.admin_oa.model.service.SysPermissionService;
import com.example.admin_oa.model.mapper.SysPermissionMapper;
import com.example.comment_oa.pojos.SysPermission;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author cjl
* @description 针对表【sys_permission】的数据库操作Service实现
* @createDate 2022-10-15 00:45:00
*/
@Service
public class SysPermissionServiceImpl extends ServiceImpl<SysPermissionMapper, SysPermission>
    implements SysPermissionService{

    @Resource
    SysPermissionMapper sysPermissionMapper;
    @Resource
    StringRedisTemplate stringRedisTemplate;

    @Override
    public List<SysPermission> meuList(int userId) {
        List<SysPermission> sysPermissions = sysPermissionMapper.meuList(userId);
        for (SysPermission sysPermission : sysPermissions) {
            sysPermission.setId(sysPermission.getPermissionId()+"");
            menuSub(sysPermission,userId);
        }
        return sysPermissions;
    }

    @Override
    public List<SysPermission> menuListAll() {
        List<SysPermission> sysPermissions = sysPermissionMapper.meneListAll();
        for (SysPermission sysPermission : sysPermissions) {
            sysPermission.setId(sysPermission.getPermissionId()+"");
            menuSubAll(sysPermission);
        }
        return sysPermissions;
    }

    @Override
    public void menuSubAll(SysPermission sysPermission) {
        List<SysPermission> sysPermissions = sysPermissionMapper.menuSubAll(sysPermission.getPermissionId());
        for (SysPermission permission : sysPermissions) {
            permission.setId(permission.getPermissionId()+"");
            menuSubAll(permission);
        }
        sysPermission.setPermissionList(sysPermissions);
    }

    @Override
    public List<SysPermission> menuYes() {
        List<SysPermission> sysPermissions = sysPermissionMapper.meneYes();
        for (SysPermission sysPermission : sysPermissions) {
            sysPermission.setId(sysPermission.getPermissionId()+"");
            menuSubAll(sysPermission);
        }
        return sysPermissions;
    }

    @Override
    public void menuSub(SysPermission sysPermission,int userId) {
        List<SysPermission> sysPermissions = sysPermissionMapper.menuSub(sysPermission.getPermissionId(),userId);
        for (SysPermission permission : sysPermissions) {
            permission.setId(permission.getPermissionId()+"");
            menuSub(permission,userId);
        }
        sysPermission.setPermissionList(sysPermissions);
    }

    @Override
    public List<SysPermission> menuList(int rolesId) {
        List<SysPermission> sysPermissions = sysPermissionMapper.menuListByRoleId(rolesId);
        for (SysPermission sysPermission : sysPermissions) {
            sysPermission.setId(sysPermission.getPermissionId()+"");
            subRole(rolesId,sysPermission);
        }
        return sysPermissions;
    }

    @Override
    public void subRole(int roleId,SysPermission sysPermission) {
        List<SysPermission> sysPermissions = sysPermissionMapper.menuSubRole(roleId, sysPermission.getPermissionId());
        for (SysPermission permission : sysPermissions) {
            permission.setId(permission.getPermissionId()+"");
            subRole(roleId,permission);
        }
        sysPermission.setPermissionList(sysPermissions);
    }

    @Override
    public Map map(int rolesId) {
        Map map=new HashMap();
        List<SysPermission> sysPermissions = menuList(rolesId);
        List<SysPermission> sysPermissions1 = meuList(1);
        map.put("all",sysPermissions1);
        map.put("yes",sysPermissions);
        return map;
    }

    @Override
    public int add(SysPermission sysPermission) {
        System.out.println(sysPermission);
        if(sysPermission.getFatherId()==null){
            System.out.println(1);
            sysPermission.setFatherId(0);
        }
        sysPermission.setPermissionState(0);
        return sysPermissionMapper.insert(sysPermission);
    }

}




